<template>
  <div class="my-adress-con">
    <div class="navbar">
      <div class="back" @click="goBack"></div>
      <div class="nav-title">我的收货地址</div>
    </div>
    <div class="block">
      <div class="location-info" v-for="(item, index) in myAdress" :key="index">
        <div class="location-left" @click="toConfirmOrder(item)">
          <div class="head">{{ item.name }}</div>
        </div>
        <div class="location-center" @click="toConfirmOrder(item)">
          <div class="user-base-info">
            <span style="font-size: 15px">{{ item.name }}</span>
            <span style="color: #b9b9b9; margin-left: 10px">{{
              item.tel
            }}</span>
          </div>
          <div class="user-location-info">
            <span>{{ item.location }}</span>
          </div>
        </div>
        <div class="location-right" @click="editor(item)">
          <img src="../../assets/img/order/editor.png" alt="" width="20px" />
        </div>
      </div>
    </div>
    <div class="bottom" @click="addAdress">
      <div class="addAdress">+ &nbsp;添加收货地址</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "",
      order_no: "",
      myAdress: [],
    };
  },
  created() {
    if (localStorage.getItem("UserInfo")) {
      this.username = window.JSON.parse(
        localStorage.getItem("UserInfo")
      ).username;
    }
    this.getAdress();
  },
  activated() {
    this.getAdress();
  },
  methods: {
    goBack() {
      this.$router.push("/idssp/order/confirmOrder");
    },
    getAdress() {
      this.myAdress = [];
      this.$http
        .get("/getAdress", {
          params: {
            username: this.username,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            this.myAdress = res.data.message;
          } else {
            this.myAdress = [];
          }
        });
    },
    editor(item) {
      this.$router.push({
        path: "/idssp/order/editorAdress",
        query: {
          id: item.id,
          name: item.name,
          tel: item.tel,
          location: item.location,
          isDefault: item.isDefault,
        },
      });
    },
    addAdress() {
      this.$router.push("/idssp/order/addAdress");
    },
    toConfirmOrder(item) {
      // 将选择的id添加到地址表中的已选择的地址的id中
      let params = {
        id: item.id,
        username: this.username,
      };
      this.$http.post("/selectAdress", params).then((res) => {
        if (res.data.errcode === 200) {
          this.$router.push("/idssp/order/confirmOrder");
        }
      });
    },
  },
};
</script>
<style lang='scss' scoped>
.my-adress-con {
  font-size: 13px;
  background-color: #eee;
  overflow: hidden;
  padding-top: 43px; //如果不写则页面会显示不完全
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    width: 100%;
    height: 44px;
    background-color: #e25b45;
    text-align: center;
    color: white;
    font-size: 16px;
    align-items: center;
    .back {
      width: 50px;
      height: 100%;
      position: absolute;
      left: 20px;
      background: url("../../assets/img/back.png") no-repeat;
      background-size: 40% 40%;
      background-position-y: center;
    }
    .nav-title {
      margin: 0 auto;
    }
  }
  .block {
    margin: 10px;
    width: 95%;
    background-color: #fff;
    border-radius: 10px;
  }
  .location-info {
    display: flex;
    justify-content: space-between;
    .location-left {
      padding-left: 10px;
      display: flex;
      align-items: center;
      .head {
        width: 40px;
        height: 40px;
        border-radius: 100%;
        background-color: #fdefe6;
        text-align: center;
        line-height: 40px;
        color: #f36d34;
      }
    }
    .location-center {
      padding: 15px;
      .user-location-info {
        padding: 5px 0;
        line-height: 14px;
        width: 200px;
      }
    }
    .location-right {
      display: flex;
      align-items: center;
      padding-right: 10px;
    }
  }
  .bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background-color: #fff;
    display: flex;
    font-size: 15px;
    .addAdress {
      margin: 6px 5px;
      background-color: #fd5e01;
      color: #fff;
      width: 100%;
      height: 34px;
      border-radius: 50px;
      text-align: center;
      line-height: 34px;
      font-size: 14px;
    }
  }
  .default {
    background: #fdf3eb;
    color: #f37d4e;
  }
}
</style>